<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\model\api;

use laytp\BaseModel;
use think\Cache;

class GenshinTeamRole extends BaseModel
{

    protected $deleteTime = 'delete_time';


    /**
     * 组队角色详情
     * @param $team_id
     * @param $position
     * @param $role_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function teamRoleInfo($team_id){
        $roles = $this->where(array(
            'team_id' => $team_id,
        ))->select()->toArray();
        $role_info = (new GenshinRole())->roleInfo();
        $weapon_info = (new GenshinWeapon())->weaponInfo();
        $relics_info = (new GenshinRelics())->cache(true)->column('relics_name as name,icon,star as stars','id');

        $return = [];
        foreach($roles as $role){
            $relics = [];
            $artifact = json_decode($role['artifact_id'],true);
            if(!empty($artifact)){
                foreach($artifact as $item){
                    $relics[] = $relics_info[$item];
                }
            }

            $return[$role['type']][] = array(
                'id'     => $role['role_id'],
                'name'   => $role_info[$role['role_id']]['role_name'],
                'avatar' => $role_info[$role['role_id']]['head_img'],
                'stand'  => $role_info[$role['role_id']]['stand'],
                'element'   => $role_info[$role['role_id']]['element'],
                'position'  => $role['position'],
                'nation'    => $role_info[$role['role_id']]['nation'],
                'stars'     => $role_info[$role['role_id']]['star'],
                'weapon'    => $weapon_info[$role['weapon_id']],
                'artifact'  => $relics,
                'mainstats' => json_decode($role['mainstats'],true),
                'substats'  => json_decode($role['substats'],true),
            );
        }
        return $return;
    }

    /**
     * 队伍角色位置
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function teamRolePosition($teamIds){
        $teamRole = $this->where([['team_id','in',implode(',',$teamIds)]])->select()->toArray();
        $rolePosition = [];
        foreach($teamRole as $item){
            $rolePosition[$item['team_id']][$item['type']][$item['role_id']] = $item['position'];
        }
        return $rolePosition;
    }
}